package com.wangtao.firstapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.ShopDetailsAdaopter;
import com.wangtao.firstapp.adapters.ShopEssayAdapter;
import com.wangtao.firstapp.adapters.ShopPriceAdapter;
import com.wangtao.firstapp.adapters.ShopWellAdapter;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.custom.ScRecyclerView;
import com.wangtao.firstapp.modles.ShopDetailsData;
import com.wangtao.firstapp.utils.DialogUtils;
import com.wangtao.firstapp.utils.HttpUtils;
import com.wangtao.firstapp.utils.ToastUtils;

/**
 * 单品详情页面
 * Created by lanouhn on 16/9/9.
 */
public class ShopDetailsActivity extends Activity implements View.OnClickListener {

    private ImageButton back;
    private ImageView imageBig, icon_PriceOne, icon_PriceTwo, shopPhoto, icon_CountryOne, icon_CountryTwo;
    private TextView titleOne, titleTwo, likes, content, price, logo, logoTwo, country, cocontent, bask, essay;
    private View viewBask, viewEssay;

    private ImageView ivLikes,ivWish;
    private TextView tvLikes,tvWish;

    private RelativeLayout rel_Price, rel_Country, relView, relEssay;
    private LinearLayout country_Content, share, wish, llt_Goumai;

    //用户晒单
    private ScRecyclerView scRecyclerView_Bask;
    private ShopDetailsAdaopter shopDetailsAdaopter;

    //实体类对象
    private ShopDetailsData shopDetailsData;

    //相关文章
    private ScRecyclerView scRecyclerView_Essay;
    private ShopEssayAdapter shopEssayAdapter;

    //相关好物
    private GridView well_GridView;
    private ShopWellAdapter shopWellAdapter;

    //参考价-->商城价格
    private ScRecyclerView price_scRecyclerView;
    private ShopPriceAdapter shopPriceAdapter;

    //商城点击事件标志位
    private int flag = 0;

    //创建自定义Dialog对象，并实例化
    DialogUtils dialogUtils = new DialogUtils(ShopDetailsActivity.this);

    //创建自定义Toast，并实例化
    ToastUtils toastUtils = new ToastUtils(ShopDetailsActivity.this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_shopdetails);
        initView();
        initData();
        getDataFromWeb();
        initEvent();
    }

    private void initView() {
        relEssay = (RelativeLayout) findViewById(R.id.relEssay_act_shopdetails);//放置  相关文章（？） 的容器
        essay = (TextView) findViewById(R.id.essay_act_shopdetails);//相关文章（？）

        bask = (TextView) findViewById(R.id.bask_act_shopdetails);//用户晒单（？）
        relView = (RelativeLayout) findViewById(R.id.relView_act_shopdetails);//放置 用户晒单（？） 的容器

        back = (ImageButton) findViewById(R.id.back_act_shopetails);//最最上部的返回图标
        imageBig = (ImageView) findViewById(R.id.imageBig_act_shopetails);//最上部的商品大图
        titleOne = (TextView) findViewById(R.id.titleOne_act_shopetails);//大图下的标题1
        titleTwo = (TextView) findViewById(R.id.titleTwo_act_shopetails);//大图下的标题2,,,拼接标题
        likes = (TextView) findViewById(R.id.likes_act_shopetails);//推荐度下的“多少人喜欢”
        content = (TextView) findViewById(R.id.content_act_shopetails);//商品介绍主内容

        rel_Price = (RelativeLayout) findViewById(R.id.rel_price_act_shopdetails);//放“参考价” ..等的容器，点击出现商城价格

        price = (TextView) findViewById(R.id.price_act_shopdetails);//商品参考价

        icon_PriceOne = (ImageView) findViewById(R.id.icon_priceOne_act_shopdetails);//“参考价”中，尖 向下的箭头。点击前显示
        icon_PriceTwo = (ImageView) findViewById(R.id.icon_priceTwo_act_shopdetails);//“参考价”中，尖 向上的箭头。点击后显示

        rel_Country = (RelativeLayout) findViewById(R.id.rel_country_act_shopdetails);//放“品牌”..容器，点击后出现品牌介绍
        shopPhoto = (ImageView) findViewById(R.id.shopPhoto_act_shopdetails);//品牌logo图
        logo = (TextView) findViewById(R.id.logo_act_shopdetails);//品牌logo字
        logoTwo = (TextView) findViewById(R.id.logoTwo_act_shopdetails);//品牌logo，后半部分
        country = (TextView) findViewById(R.id.country_act_shopdetails);//品牌所属国家

        icon_CountryOne = (ImageView) findViewById(R.id.icon_countryOne_act_shopdetails);//“品牌”中，尖 向下的箭头。点击前显示
        icon_CountryTwo = (ImageView) findViewById(R.id.icon_countryTwo_act_shopdetails);//“品牌”中，尖 向上的箭头。点击后显示
        country_Content = (LinearLayout) findViewById(R.id.country_content_act_shopdetails);//品牌介绍主内容，点击rel_Country后出现容器
        cocontent = (TextView) findViewById(R.id.country_cocontent_act_shopdetails);//品牌介绍主内容，点击rel_Country后出现,,文本
        scRecyclerView_Bask = (ScRecyclerView) findViewById(R.id.scRecyclerView_bask_act_shopdetails);//用户晒单...
        scRecyclerView_Essay = (ScRecyclerView) findViewById(R.id.scRecyclerView_essay_act_shopdetails);//相关文章...
        well_GridView = (GridView) findViewById(R.id.well_gridView_act_shopdetails);//相关好物
        share = (LinearLayout) findViewById(R.id.share_act_shopdetails);//分享
        llt_Goumai = (LinearLayout) findViewById(R.id.llt_goumai_act_shopdetails);//购买
        wish = (LinearLayout) findViewById(R.id.wish_act_shopdetails);//收入心愿单

        price_scRecyclerView = (ScRecyclerView) findViewById(R.id.price_scRecyclerView_act_shopdetails);//商城价格
        viewBask = findViewById(R.id.viewBask_act_shopdetails);//
        viewEssay = findViewById(R.id.viewEssay_act_shopdetails);//

        //点击前
        ivLikes = (ImageView) findViewById(R.id.ivLikes_act_shopdetails);//
        tvLikes = (TextView) findViewById(R.id.tvLikes_act_shopdetails);//

        //点击后
        ivWish = (ImageView) findViewById(R.id.ivWish_act_shopdetails);//
        tvWish= (TextView) findViewById(R.id.tvWish_act_shopdetails);//
    }

    private void initData() {

    }

    private void getDataFromWeb() {

        Intent intent = getIntent();
        String shop_ID = intent.getStringExtra("shop_iD");
        String urlShop = "http://open4.bantangapp.com/products/brandProduct/info";
        String paramsShop = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + shop_ID;

        try {
            HttpUtils.doPostAsyn(urlShop, paramsShop, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_shop = new Gson();
                    shopDetailsData = gson_shop.fromJson(result, ShopDetailsData.class);
                    if (shopDetailsData.getData() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:

                    if (shopDetailsData.getData().getPost_count().equals("")) {
                        viewBask.setVisibility(View.GONE);
                    }
                    if (shopDetailsData.getData().getArticle_topic_count().equals("")) {
                        viewEssay.setVisibility(View.GONE);
                    }
                    Picasso.with(ShopDetailsActivity.this).load(shopDetailsData.getData().getPics().get(0)).into(imageBig);
                    titleOne.setText(shopDetailsData.getData().getBrand().getName());
                    titleTwo.setText(shopDetailsData.getData().getTitle());
                    likes.setText(shopDetailsData.getData().getLikes() + "人喜欢");
                    content.setText(shopDetailsData.getData().getDesc());
                    price.setText(shopDetailsData.getData().getPrice());
                    Picasso.with(ShopDetailsActivity.this).load(shopDetailsData.getData().getBrand().getPic()).into(shopPhoto);
                    logo.setText(shopDetailsData.getData().getBrand().getName());
                    country.setText(shopDetailsData.getData().getBrand().getCountry());

                    if (!shopDetailsData.getData().getBrand().getAlias().equals("")) {
                        logoTwo.setText(" / " + shopDetailsData.getData().getBrand().getAlias());
                    }

//                    if (country_Content.getChildCount() > 0)
//                        country_Content.removeAllViews();
//                    TextView textView = new TextView(ShopDetailsActivity.this);
//                    textView.setText(shopDetailsData.getData().getBrand().getDesc());
//                    country_Content.addView(textView);

                    cocontent.setText(shopDetailsData.getData().getBrand().getDesc());

                    if (!shopDetailsData.getData().getPost_count().equals("")){
                        relView.setVisibility(View.VISIBLE);
                        bask.setText("用户晒单 "+"（" + shopDetailsData.getData().getPost_count() + "）");
                        //用户晒单
                        shopDetailsAdaopter = new ShopDetailsAdaopter(ShopDetailsActivity.this, shopDetailsData);
                        scRecyclerView_Bask.setAdapter(shopDetailsAdaopter);
                        scRecyclerView_Bask.setLayoutManager(new FullyLinearLayoutManager(ShopDetailsActivity.this));
                    }

                    if (!shopDetailsData.getData().getArticle_topic_count().equals("")){
                        relEssay.setVisibility(View.VISIBLE);
                        essay.setText("相关文章 " + "（" + shopDetailsData.getData().getArticle_topic_count() + "）");
                        //相关文章
                        shopEssayAdapter = new ShopEssayAdapter(ShopDetailsActivity.this, shopDetailsData);
                        scRecyclerView_Essay.setAdapter(shopEssayAdapter);
                        scRecyclerView_Essay.setLayoutManager(new FullyLinearLayoutManager(ShopDetailsActivity.this));
                    }


                    int size = shopDetailsData.getData().getProduct_list().size();
                    int length = 100;
                    DisplayMetrics dm = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(dm);
                    float density = dm.density;
                    int gridviewWidth = (int) (size * (length + 4) * density);
                    int itemWidth = (int) (length * density);

                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
                    well_GridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
                    well_GridView.setColumnWidth(itemWidth); // 设置列表项宽
                    // well_GridView.setHorizontalSpacing(5); // 设置列表项水平间距
                    well_GridView.setStretchMode(GridView.NO_STRETCH);
                    well_GridView.setNumColumns(size); // 设置列数量=列表集合数

                    //相关好物
                    shopWellAdapter = new ShopWellAdapter(ShopDetailsActivity.this, shopDetailsData.getData().getProduct_list());
                    well_GridView.setAdapter(shopWellAdapter);

                    //商城价格
                    shopPriceAdapter = new ShopPriceAdapter(ShopDetailsActivity.this, shopDetailsData.getData().getPurchase_way());
                    price_scRecyclerView.setAdapter(shopPriceAdapter);
                    price_scRecyclerView.setLayoutManager(new FullyLinearLayoutManager(ShopDetailsActivity.this));
                    break;

            }
        }
    };

    private void initEvent() {
        back.setOnClickListener(this);
        rel_Price.setOnClickListener(this);
        rel_Country.setOnClickListener(this);
        relView.setOnClickListener(this);
        relEssay.setOnClickListener(this);
        wish.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            //点击出现商城价格...
            case R.id.rel_price_act_shopdetails:
                if (flag == 0) {
                    icon_PriceOne.setVisibility(View.GONE);
                    icon_PriceTwo.setVisibility(View.VISIBLE);
                    price_scRecyclerView.setVisibility(View.VISIBLE);
                    flag = 1;
                } else {
                    icon_PriceOne.setVisibility(View.VISIBLE);
                    icon_PriceTwo.setVisibility(View.GONE);
                    price_scRecyclerView.setVisibility(View.GONE);
                    flag = 0;
                }
                break;

            //点击出现品牌介绍...
            case R.id.rel_country_act_shopdetails:
                if (flag == 0) {
                    icon_CountryOne.setVisibility(View.GONE);
                    icon_CountryTwo.setVisibility(View.VISIBLE);
                    country_Content.setVisibility(View.VISIBLE);
                    flag = 1;
                } else {
                    icon_CountryOne.setVisibility(View.VISIBLE);
                    icon_CountryTwo.setVisibility(View.GONE);
                    country_Content.setVisibility(View.GONE);
                    flag = 0;
                }
                break;

            //返回上一个页面
            case R.id.back_act_shopetails:
                finish();
                break;

            //跳转至 全部晒单 页面
            case R.id.relView_act_shopdetails:
                String details_ID = shopDetailsData.getData().getId();
                Intent intent = new Intent(ShopDetailsActivity.this, AllBatheActivity.class);
                intent.putExtra("details_iD", details_ID);
                startActivity(intent);

                break;

            //跳转至 全部文章 页面
            case R.id.relEssay_act_shopdetails:

                String essay_ID = shopDetailsData.getData().getId();
                Intent intent1 = new Intent(ShopDetailsActivity.this, AllEssayActivity.class);
                intent1.putExtra("essay_iD", essay_ID);
                startActivity(intent1);
                break;

            // //点击后，切换视图，二次点击，弹出Dialog
            case R.id.wish_act_shopdetails:
                if (flag==0){
                    wish.setBackgroundColor(Color.WHITE);
                    ivLikes.setVisibility(View.GONE);
                    tvLikes .setVisibility(View.GONE);
                    ivWish .setVisibility(View.VISIBLE);
                    tvWish.setVisibility(View.VISIBLE);
                    toastUtils.toast(shopDetailsData.getData().getPics().get(0), 1000);
                    flag = 1;
                }else {
                    dialogUtils.dialog(shopDetailsData.getData().getPics().get(0), onClickListener);
                }
                break;
        }

    }

    //回调接口，监听Dialog
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                //退出Dialog，在Activity中写取消喜欢
                case R.id.cancel_likes_wish_dialog:
                    dialogUtils.cancelBtn();
                    wish.setBackgroundColor(Color.RED);
                    ivLikes.setVisibility(View.VISIBLE);
                    tvLikes .setVisibility(View.VISIBLE);
                    ivWish .setVisibility(View.GONE);
                    tvWish.setVisibility(View.GONE);
                    flag = 0;
                    break;

                //退出Dialog
                case R.id.bad_likes_wish_dialog:
                    dialogUtils.badIma();
                    break;

                //退出Dialog，并Toast
                case R.id.lltLike_likes_wish_dialog:
                    toastUtils.toast(shopDetailsData.getData().getPics().get(0), 1000);
                    dialogUtils.lltLike();
                    break;

                //跳转至，编辑心愿单页面
                case R.id.found_likes_wish_dialog:

                    dialogUtils.found();
                    break;
            }
        }
    };
}
